<?php
/**
 * Created by PhpStorm.
 * User: guokunsheng
 * Date: 18/6/6
 * Time: 17:35
 */

namespace app\api\controller\v2;

use app\api\controller\Common;
use app\common\cachemodel\AppConfig;
use app\common\lib\Alidayu;

/**
 * @title 短信验证码
 * @desc
 * @url
 * @version 2.0.0
 * Class Identify
 * @package app\api\controller\v2
 */
class Identify extends Common
{
    protected $restActionList = ['save'];

    /**
     * post
     * 设置短信验证码
     */
    public function save()
    {
        if (!request()->isPost())
        {
            return show(config('code.error'), '未知操作', [], 403);
        }
        //  数据校验
        $validate = validate('Identify');

        if (!$validate->check(input('post.')))
        {
            return show(config('code.error'), $validate->getError(), [], 403);
        }

        $tel = input('param.tel/s');// 手机号

        $params = rand(100000, 999999);// 验证码

        $alidayu = (new AppConfig())->configInfo('alidayu');
        $code = $alidayu['login_code'];// 短信登录模版

        if (Alidayu::getInstance()->sendSms($tel, $params, $code))
        {
            return show(config('code.success'), '发送成功');
        } else
        {
            return show(config('code.error'), "发送失败");
        }
    }

    /**
     * 参数规则
     * @name 字段名称
     * @type 类型
     * @require 是否必须
     * @default 默认值
     * @desc 说明
     * @range 范围
     * @return array
     */
    public static function getRules()
    {
        $rules = [
            'save' => [
                'tel' => ['name' => 'tel', 'type' => 'int', 'require' => 'true', 'default' => '无', 'desc' => '手机号', 'range' => '11位']
            ],
        ];
        return $rules;
    }
}